package de.lijin.databank;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataFromMethDB {

	private Context myContext;
	private MyDatabaseHelper myDbHelper;
	private SQLiteDatabase myDataBase;

	public DataFromMethDB(Context context) {
		myContext = context;
		myDbHelper = new MyDatabaseHelper(myContext);
	}

	public void openDB() {
		myDbHelper.openDataBase();
		myDataBase = myDbHelper.getMyDataBase();
	}

	public void closeDB() {
		myDbHelper.close();
	}

	public String getBezeichnug(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "bezeichnung" }, "_id = " + id, null, null,
				null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			if (cursor.getString(0) != null)
				str = cursor.getString(0).split(";")[0];
		}
		return str;
	}

	public String getAbstract(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "abstract" }, "_id = " + id, null, null, null,
				null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		return str;
	}

	public String[] getVorteile(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "vorteile" }, "_id = " + id, null, null, null,
				null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public String[] getNachteile(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "nachteile" }, "_id = " + id, null, null, null,
				null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public String[] getInputs(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE", new String[] { "input" },
				"_id = " + id, null, null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public String[] getOutputs(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE", new String[] { "output" },
				"_id = " + id, null, null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public Ablauf getSchritte(int methoden_id) {

		Cursor cursor = myDataBase.query("ABLAUF", null, "METHODE_ID = "
				+ methoden_id, null, null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			return new Ablauf(cursor);
		}
		return null;
	}

	public String getKurzBeschreibung(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "kurzbeschreibung" }, "_id = " + id, null, null,
				null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		return str;
	}

	public TreeMap<String, Integer> getMethodenList() {
		Map<String, Integer> list = new HashMap<String, Integer>();
		Cursor cursor = myDataBase.query("METHODE", new String[] {
				"bezeichnung", "_id" }, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			list.put(cursor.getString(0), Integer.valueOf(cursor.getInt(1)));
			cursor.moveToNext();
		}
		return new TreeMap<String, Integer>(list);
	}

	public String getAlternativen(int id) {// muss korrigiert werden
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "alternativen" }, "_id = " + id, null, null,
				null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		return str;
	}

	public String[] getHilfsmittel(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "hilfsmittel" }, "_id = " + id, null, null,
				null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public SuchErgebnis findMethod(String bezeichnung) {
		SuchErgebnis ergebnis = new SuchErgebnis(-1, bezeichnung, false);
		TreeMap<String, Integer> tp = getMethodenList();
		for (String s : tp.keySet()) {
			if (s.equals(bezeichnung)) {
				ergebnis.setId(tp.get(s));
				ergebnis.setVorhanden(true);
				break;
			}
		}

		return ergebnis;
	}

	public String[] getAlternativeMethoden(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "alternativen" }, "_id = " + id, null, null,
				null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public String[] getAnwendungsbeispiele(int id) {
		String str = null;
		Cursor cursor = myDataBase.query("METHODE",
				new String[] { "anwendungsbeispiel" }, "_id = " + id, null,
				null, null, null);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			str = cursor.getString(0);
		}
		if (str != null) {
			return str.split(";");
		}
		return null;
	}

	public ArrayList<Method> getBestMethods(TreeSet<Integer> ts) {
		int[] indexOfSpaltenInDB = new int[] { 2, 2, 3, 4, 4, 5, 6 };
		int[] indexOfPEPsInDB = new int[] { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
		ArrayList<Method> list = new ArrayList<Method>();
		ArrayList<ArrayList<Integer>> peps = new ArrayList<ArrayList<Integer>>();
		for (int i = 0; i < 10; i++) {
			peps.add(new ArrayList<Integer>());
		}
		for (Integer i : ts) {
			int pep = i / 10;
			int spalten;
			if(i>=10){
				spalten = i % (pep * 10);
			}else{
				spalten = i;
			}
			peps.get(pep).add(Integer.valueOf(spalten));
		}
		HashMap<Integer, Integer> ergebnisse = new HashMap<Integer, Integer>();// list [ id, summe]
//		Cursor cursor = myDataBase.query("BEWERTUNG", null, null, null, null,
//				null, null);
		Cursor cursor = myDataBase.rawQuery("select * from BEWERTUNG",null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
//			Log.v("id", String.valueOf(cursor.getInt(0)));
			
			int summe = 0;
			outerloop:
			for (ArrayList<Integer> pep : peps) {
				if (pep.size() > 0 && cursor.getInt(indexOfPEPsInDB[peps.indexOf(pep)])!=0 ) {
//					Log.v("pep", String.valueOf(peps.indexOf(pep)));
					
					for (Integer spalte : pep) {
//						Log.v("spalte", String.valueOf(spalte));
						if(cursor.getInt(indexOfSpaltenInDB[spalte])==0 ){
							summe = 0;
							break outerloop;
						}
//						Log.v("spalte faktor", String.valueOf(cursor.getInt(indexOfPEPsInDB[peps.indexOf(pep)])));
						summe += cursor.getInt(indexOfPEPsInDB[peps
								.indexOf(pep)])
								* cursor.getInt(indexOfSpaltenInDB[spalte]);
					}
//					Log.v("Summe", String.valueOf(summe));

				}
			}
			if(summe != 0){
//				Log.v("summe", String.valueOf(summe));
				list.add(new Method(cursor.getInt(1), getBezeichnug(cursor.getInt(1)), 0));
//				ergebnisse.put(summe,cursor.getInt(1));
			}
			cursor.moveToNext();
		}
//		TreeMap<Integer, Integer> ergebnisseSortiert = new TreeMap<Integer, Integer>(ergebnisse);
//
//		for(int i = ergebnisseSortiert.size()-1;i>=0;i--){
//			int _id = ((Integer)ergebnisseSortiert.values().toArray()[i]);
////			Log.v("id", String.valueOf(_id));
//			list.add(new Method(_id, getBezeichnug(_id), 0));
//		}
		return list;
	}
}
